//
//  addViewController.m
//  LazyCat
//
//  Created by rocky on 15/12/30.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "addViewController.h"
#import "fatherClassifyViewController.h"
#import "AFNetworkHandler.h"

@interface addViewController ()<UITextFieldDelegate,backFuStrDelegate>

@property (nonatomic,strong) UITextField *textField3;//父类
@property (nonatomic,strong) NSString *one;//分类名字
@property (nonatomic,strong) NSString *two;//排序值
@property (nonatomic,strong) NSString *three;//父分类名字
@property (nonatomic,strong) NSString *four;//是否激活
@property (nonatomic,strong) NSString *five;//父分类id

@end

@implementation addViewController

-(void)backFuStr:(NSString *)str identify:(NSString *)idf {
    self.textField3.text = [NSString stringWithFormat:@"%@",str];
    self.three = str;
    self.five = idf;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav:@"添加分类" doBack:YES];
    UIView *lineV= [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)] ;
    lineV.backgroundColor = [UIColor lightGrayColor];
    lineV.alpha = 0.2;
    [self.navBar addSubview:lineV];
    self.view.backgroundColor = BackColor;
    [self cretView];//创建视图
    self.five = @"";
}
-(void)cretView {
    //第一行
    UILabel *lable1 = [[UILabel alloc] initWithFrame:CGRectMake(0,65 , ScreenWidth, 50)];
    lable1.backgroundColor = [UIColor whiteColor];
    lable1.text = @"  分类名称";
    lable1.font = [UIFont fontWithName:nil size:14];
    [self.view addSubview:lable1];
    UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(80, 65, ScreenWidth - 80, 50)];
    textField1.tag = 100;
    textField1.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField1.delegate = self;
    textField1.font = [UIFont fontWithName:nil size:14];
    textField1.placeholder = @"请填写分类名称";
    [self.view addSubview:textField1];
    //第二行
    UILabel *lable2 = [[UILabel alloc] initWithFrame:CGRectMake(0,65 + 51 , ScreenWidth, 50)];
    lable2.backgroundColor = [UIColor whiteColor];
    lable2.text = @"  排序值";
    lable2.font = [UIFont fontWithName:nil size:14];
    [self.view addSubview:lable2];
    UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(80, 65 + 51, ScreenWidth - 80, 50)];
    textField2.tag = 200;
    textField2.delegate = self;
    textField2.keyboardType = UIKeyboardTypeNumberPad;
    textField2.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField2.font = [UIFont fontWithName:nil size:14];
    textField2.placeholder = @"排序值越大越靠前";
    [self.view addSubview:textField2];
    //第三行
    UILabel *lable3 = [[UILabel alloc] initWithFrame:CGRectMake(0,65 + 51+51 , ScreenWidth, 50)];
    lable3.backgroundColor = [UIColor whiteColor];
    lable3.text = @"  父级分类";
    lable3.font = [UIFont fontWithName:nil size:14];
    [self.view addSubview:lable3];
    self.textField3 = [[UITextField alloc] initWithFrame:CGRectMake(80, 65 + 51 + 51, ScreenWidth - 110, 50)];
    self.textField3.tag  = 300;
    self.textField3.delegate = self;
    [self.textField3 setEnabled:NO];//不可编辑
    self.textField3.font = [UIFont fontWithName:nil size:14];
    self.textField3.placeholder = @"添加一级分类请留空";
    [self.view addSubview:self.textField3];
    lable3.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jump:)];//跳到所有父类
    [lable3 addGestureRecognizer:tap];
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth-23, 65 + 51 + 51 +17, 10, 15)];
    imageV.alpha = 0.5;
    imageV.image = [UIImage imageNamed:@"huijiantou"];
    [self.view addSubview:imageV];
    UILabel *lable4 = [[UILabel alloc] initWithFrame:CGRectMake(0,65 + 51+51+51 , ScreenWidth, 50)];
    //第四行
    lable4.backgroundColor = [UIColor whiteColor];
    lable4.text = @"  启用当前分类";
    lable4.font = [UIFont fontWithName:nil size:14];
    [self.view addSubview:lable4];
    UISwitch *swith = [[UISwitch alloc] initWithFrame:CGRectMake(ScreenWidth - 60, 65 + 51+51+61, 60, 30)];
    swith.onTintColor = MainColorT;
    swith.on = YES;
    self.four = @"1";
    [swith addTarget:self action:@selector(swithAction:) forControlEvents:UIControlEventValueChanged];//开关点击事件
    [self.view addSubview:swith];
    UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(10,  65+51+51+126, ScreenWidth - 20, 45)];
    addButton.layer.cornerRadius = 5;
    [addButton setTitle:@"添加" forState:UIControlStateNormal];
    [addButton setBackgroundColor:MainColorT];
    [addButton addTarget:self action:@selector(addtypeAction) forControlEvents:UIControlEventTouchUpInside];//点击保存按钮
    [self.view addSubview:addButton];
}
-(void)jump:(UITapGestureRecognizer *)tap {//点击跳转到所有父类页面
    if (self.HeadArr.count > 0) {
        fatherClassifyViewController *fatherVC = [[fatherClassifyViewController alloc]init];
        fatherVC.faArr = self.HeadArr;//传递所有父类的数组
        fatherVC.delegate = self;
        fatherVC.faStr = self.three;//传递父类的名字用于加对号
        [self.navigationController pushViewController:fatherVC animated:YES];
    }else {
        [[MessShowView share] showWithMess:@"暂时还没有父级分类"];
    }
}
-(void)addtypeAction {//点击保存按钮
    [self.view endEditing:YES];
    if (self.one.length == 0) {
        [[MessShowView share] showWithMess:@"请输入分类名称" login:NO];
        return;
    }
    NSString *url = [NSString stringWithFormat:addClassifyUrl,self.one,self.two,self.five,self.four,[AppData share].username];
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        NSDictionary *dic = result;
        NSString *r = dic[@"r"];
        if ([r isEqualToString:@"T"]) {
            [[MessShowView share] showWithMess:@"新增分类成功" login:YES];
            [self.delegate AddValue:@"m"];
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            [[MessShowView share] showWithMess:@"新增分类失败" login:NO];
        }
    }];
}
-(void)swithAction:(UISwitch *)swch {//开关按钮
    if (swch.on) {
        self.four = @"1";
    }else{
        self.four = @"0";
    }
}
#pragma mark - UITextField
-(void)textFieldDidEndEditing:(UITextField *)textField {
    [textField resignFirstResponder];
    if (textField.tag == 100) {
        self.one = textField.text;
    }else if (textField.tag == 200){
        self.two = textField.text;
    }else if (textField.tag == 300){
        self.three = textField.text;
    }
}
- (BOOL)textFieldShouldClear:(UITextField *)textField {
    return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
